/***
 * @Description:
 * @Author: LI Yihong
 * @Email: liyh@jihua.ac.cn
 * @Date: 2025-02-26 08:40:13
 * @LastEditTime: 2025-02-26 08:41:25
 * @LastEditors: LI Yihong
 * @FilePath: \STM32F407_AD9910_Control\applications\AD9910\AD9910_Thread.hpp
 */
#pragma once
#include "cxx_thread.h"
#include <board.h>
#include <rtdevice.h>
#include "AD9910_RAM.hpp"
#include <memory>

using namespace std;
using namespace rtthread;

class TCP_SERVER;

enum class CMD_HEAD : uint8_t
{
    SetProfile = 0x01,
    SetRamData = 0x02,
    SetEnable = 0x03,
    SetUpdate = 0x04,
    SetRamDemo = 0x05,
    readRamData = 0x06,
    SetTimer = 0x07
};

class AD9910_Thread : public Thread
{
public:
    AD9910_RAM *m_ad9910_ram;
    TCP_SERVER *m_tcp_server;
    rt_device_t hw_dev; /* 定时器设备句柄 */

    profile_t m_profile;
    std::vector<uint32_t> m_RAM_data;

    static AD9910_Thread &getInstance();

    void run(void *parameter) override;

    rt_err_t hwTimerConfig();
    rt_err_t hwTimerstart(const rt_int32_t sec, const rt_int32_t usec);
    rt_err_t hwTimerstop();
    static rt_err_t hwTimerCallback(rt_device_t dev, rt_size_t size);

    void processCmd(const std::vector<uint8_t> *cmdData);
    void setProfile(const std::vector<uint8_t> *cmdData);
    void setRamData(const std::vector<uint8_t> *cmdData);
    void readRamData(const std::vector<uint8_t> *cmdData);
    void setEnable(const std::vector<uint8_t> *cmdData);
    void setUpdate(const std::vector<uint8_t> *cmdData);
    void setRamDemo(const std::vector<uint8_t> *cmdData);
    void setTimer(const std::vector<uint8_t> *cmdData);

private:
    AD9910_Thread(rt_uint32_t stack_size,
                  rt_uint8_t priority,
                  rt_uint32_t tick,
                  const char *name);
    ~AD9910_Thread();
    AD9910_Thread(const AD9910_Thread &) = delete;
    AD9910_Thread &operator=(const AD9910_Thread &) = delete;
};
